<template>
	<view class="content">
		<view class="form">
			<u--form ref="uForm" errorType="none" :rules="rules" :model="form" labelPosition="top" labelWidth="80" :labelStyle="{color: '#ffffff', fontSize: '25rpx', paddingBottom: '10rpx'}">
				<u-form-item label="邮箱" prop="email">
					<u--input class="input" v-model="form.email" color="#ffffff"></u--input>
				</u-form-item>
				<u-form-item>
					<button class="light-btn btn" @click="onUpdate">立即设置</button>
				</u-form-item>
			</u--form>
		</view>
	</view>
</template>

<script>
	import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
	export default {
		components: {
		},
		data() {
			return {
				userInfo: {},
				form: {
					email: null
				},
				rules: {
					'email': [
						{
							type: 'string',
							required: true,
							message: '请输入密码',
							trigger: ['change']
						},
						{
							validator: (rule, value, callback) => {
								return uni.$u.test.email(value);
							},
							message: '邮箱不正确',
							trigger: ['change']
						}
					]
				}
			}
		},
		onLoad() {
			this.getInfo()
		},
		methods: {
			...mapActions('user', ['updateUserEmail', 'getUserInfo']),
			getInfo(){
				this.getUserInfo().then(res => {
					this.userInfo = res.data
					this.form.email = this.userInfo.email
				})
			},
			onUpdate(){
				this.$refs.uForm.validate().then(res => {
					this.updateUserEmail(this.form).then(r => {
						uni.$u.toast('设置成功')
						this.getInfo()
					})
				}).catch(errors => {
					uni.$u.toast(errors[0].message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		.form{
			padding: 20rpx 20rpx;
			.input{
				background-color: #232639 !important;
				border: 1rpx solid #666666 !important;
				color: #ffffff;
			}
		}
		.btn{
			margin-top: 20rpx;
			width: 50%;
		}
	}
</style>
